/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.recitation.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.recitation.domain.Recitation;
import me.zhengjie.recitation.domain.vo.RecitationQueryCriteria;
import me.zhengjie.recitation.service.RecitationService;
import me.zhengjie.recitation.util.FileMeta;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author yellow
* @date
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "recitation管理")
@RequestMapping("/api/recitation")
public class RecitationController {

    private final RecitationService recitationService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportRecitation(HttpServletResponse response, RecitationQueryCriteria criteria) throws IOException {
        recitationService.download(recitationService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询recitation")
    @ApiOperation("查询recitation")
    @AnonymousAccess
    public ResponseEntity<PageResult<Recitation>> queryRecitation(RecitationQueryCriteria criteria, Page<Object> page ){
        return new ResponseEntity<>(recitationService.findAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value = "/paged")
    @Log("分页查询recitation")
    @ApiOperation("分页查询recitation")
    @AnonymousAccess
    public ResponseEntity<PageResult<Recitation>> pagedQueryRecitation(RecitationQueryCriteria criteria,
                                                                  @RequestParam(defaultValue = "1") int current,
                                                                  @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(recitationService.findAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping("/sftp")
    @Log("上传recitation")
    @ApiOperation("上传recitation")
    @AnonymousAccess
    public ResponseEntity<FileMeta> uploadRecitation(
            @RequestParam("file") MultipartFile file,
            @Validated @ModelAttribute Recitation resources
    ) {
        FileMeta meta = recitationService.upload(resources,file);
        return ResponseEntity.ok(meta);
    }

    @PostMapping
    @Log("上传recitation")
    @ApiOperation("上传recitation")
    @AnonymousAccess
    public ResponseEntity<FileMeta> uploadRecitationR2(
            @RequestParam("file") MultipartFile file,
            @Validated @ModelAttribute Recitation resources
    ) {
        FileMeta meta = recitationService.uploadR2(resources, file);
        return ResponseEntity.ok(meta);
    }

    @PatchMapping
    @Log("修改recitation")
    @ApiOperation("修改recitation")
    @AnonymousAccess
    public ResponseEntity<Object> updateRecitation(
            @RequestParam("file") MultipartFile file,
            @Validated @ModelAttribute Recitation resources){
        FileMeta meta = recitationService.update(resources,file);
        return ResponseEntity.ok(meta);
    }

    @DeleteMapping
    @Log("删除recitation")
    @ApiOperation("删除recitation")
    @AnonymousAccess
    public ResponseEntity<Object> deleteRecitation(@RequestParam("recitationId") Integer recitationId) {
        recitationService.delete(recitationId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping(value = "/play")
    @Log("播放recitation")
    @ApiOperation("播放recitation")
    @AnonymousAccess
    public ResponseEntity<Object> playRecitation  (
            @RequestParam("recitationId") Integer recitationId,HttpServletResponse response) {
        return recitationService.playRecitationById(recitationId);
    }

    @GetMapping("/my")
    @Log("查询我的朗读")
    @ApiOperation("查询我的朗读")
    @AnonymousAccess
    public List<Recitation> queryMyPost(@RequestParam String userName){
        return recitationService.getRecitationByUserName(userName);
    }

    @GetMapping("/getById")
    @Log("根据主键查询recitation")
    @ApiOperation("根据主键查询recitation")
    @AnonymousAccess
    public Recitation getPostById(@RequestParam Integer recitationId){
        return recitationService.getRecitationById(recitationId);
    }

    @GetMapping("/all")
    @Log("查询所有recitation")
    @ApiOperation("查询所有recitation")
    @AnonymousAccess
    public List<Recitation> getAll(){
        return recitationService.getAll();
    }

    @GetMapping("/favorite")
    @Log("查询我收藏的recitation")
    @ApiOperation("查询我收藏的recitation")
    @AnonymousAccess
    public List<Recitation> getMyFavoriteRecitation(@RequestParam String userName){
        return recitationService.getMyFavoriteRecitation(userName);
    }
}